package edu.mse.camel.client.whiteboard.collaboration;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.RollbackException;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.workspace.AbstractEMFOperation;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.gef.commands.CommandStackEventListener;
import org.eclipse.gef.commands.CommandStackListener;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramCommandStack;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramEditDomain;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.gef.commands.Command;

import edu.mse.camel.client.session.team.ITeamMember;
import edu.mse.camel.client.session.team.TEAMMEMBER_STATUS;
import edu.mse.camel.client.session.team.TeamList;

public class WhiteboardCommandStack extends DiagramCommandStack {
	private ResourceSet root;
	private volatile boolean shouldRecord = true;
	private int tokenID;
	WB_CamelChangeRecorder2 recorder;
	TransactionalEditingDomain transactionalEditingDomain;
	
	public WhiteboardCommandStack(IDiagramEditDomain editDomain, TransactionalEditingDomain editingDomain, ResourceSet resourceSet, int tokenID) {
		super(editDomain);
		//.DiagramCommandStack(originalCommandStack.getDiagramEditDomain());
		//this.originalCommandStack = editDomain.getDiagramCommandStack();
		this.root = resourceSet;
		this.tokenID = tokenID;
		this.transactionalEditingDomain = editingDomain;
	}

	private void PreDo () {
		
		ITeamMember currentUser = TeamList.getInstance().getMe();
		if(null != currentUser) {
			if(TEAMMEMBER_STATUS.SESSION == currentUser.getStatus()) {
				// we are in the session
				

			}
		}
		
		if(getShouldRecord()) {
			recorder = new WB_CamelChangeRecorder2(root);
			recorder.beginRecording();
		}
		else {
		}
	}
	
	private class EMFOperation extends AbstractEMFOperation {
		
		public EMFOperation(TransactionalEditingDomain domain, String label) {
			super(domain, label);
		}

		@Override
		protected IStatus doExecute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
			if(recorder != null) {
				if(recorder.IsRecording()) {
					recorder.publish();
					recorder.dispose();
					return Status.OK_STATUS;
				}
			}
			return Status.CANCEL_STATUS;
		}

	}
	
	private void PostDo () {
		EMFOperation operation = new EMFOperation(transactionalEditingDomain, "hello recorder label");
		
		try {
			this.getOperationHistory().execute(operation, null, null);
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void execute(Command command) {
		// TODO Auto-generated method stub
		System.out.println("before executing : " + command + "-" + command.getDebugLabel());
		PreDo();
		super.execute(command);
		PostDo();
		
		System.out.println("after executing : " + command);
	}

	private void setShouldRecord(boolean shouldRecord) {
		this.shouldRecord = shouldRecord;
	}

	private boolean getShouldRecord() {
		return shouldRecord;
	}

}
